package com.itbaizhan.service.impl;

import com.itbaizhan.common.BaseResponse;
import com.itbaizhan.service.*;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

@Service
public class IBusinessServiceImpl implements IBusinessService  {

    @DubboReference
    private ITStockTCCService itStockService; // 库存


    @DubboReference
    private ITOrderTCCService itOrderService; // 订单

    /**
     * 创建订单
     * @param userId
     * @param commodityCode
     * @param count
     * @param money
     * @return
     */
    @GlobalTransactional // 全局事务
    @Override
    public BaseResponse save(String userId, String commodityCode, Integer count, Integer money) {
        // 1、扣减库存
        BaseResponse stockRes = itStockService.deduct(commodityCode, count);
        // 2、创建订单
        BaseResponse orderRes = itOrderService.save(userId, commodityCode, count, money);
        if ( orderRes.getCode() == 200 && stockRes.getCode() == 200 ){
            return BaseResponse.success();
        }
        return BaseResponse.error();
    }


}
